﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using W.Product.API;
using W.Product.Domain.Dto;
using W.Product.Domain.Product;
using W.Product.Read.API.Appliaction.Command.ProductCommand;
using W.Product.Read.API.Appliaction.Command.ProductTypeCommand;


namespace W.Product.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]

    //产品显示，产品类型显示控制器
    public class ProductSelectController : ControllerBase
    {
        private readonly IMediator mediator;
        private ILogger<ProductSelectController> logger;


        public ProductSelectController(IMediator mediator, ILogger<ProductSelectController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 显示查询产品信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProductPage>> GetProductInfos([FromQuery]ProductSelectCommand command)
        {
            ApiResult<ProductPage> result = new ApiResult<ProductPage>();
            try
            {
                logger.LogInformation("产品类型父类");
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 显示产品类别/查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProductTypePages>> GetProductType([FromQuery] ProductTypeSelectCommand command)
        {
            ApiResult<ProductTypePages> result = new ApiResult<ProductTypePages>();
            try
            {
                logger.LogInformation("显示产品，查询");
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 显示产品类型父类
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ProductType>>> GetProductTypesss()
        {
            ApiResult<List<ProductType>> result = new ApiResult<List<ProductType>>();
           
            try
            {
                logger.LogInformation("显示产品类别，查询");
                result.Data = await mediator.Send(new ProductTypeSelCommand());
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 批量反填产品
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<ProductDto>>> FanRangeProductInfo(ProductRangeFanCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 导出产品管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ProductExcel([FromQuery] ProductSelectCommand command)
        {
            var list = await mediator.Send(command);

            IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("产品管理");

            IRow rowl = sheet.CreateRow(0);
            rowl.CreateCell(0).SetCellValue("产品编号");
            rowl.CreateCell(1).SetCellValue("产品名称");
            rowl.CreateCell(2).SetCellValue("厂家编码");
            rowl.CreateCell(3).SetCellValue("内部编码");
            rowl.CreateCell(4).SetCellValue("规格");
            rowl.CreateCell(5).SetCellValue("类别");
            rowl.CreateCell(6).SetCellValue("存储单位");
            rowl.CreateCell(7).SetCellValue("预警（下）");
            rowl.CreateCell(8).SetCellValue("预警（上）");
            rowl.CreateCell(9).SetCellValue("包装类型");
            rowl.CreateCell(10).SetCellValue("价格");
            rowl.CreateCell(11).SetCellValue("重量");
            rowl.CreateCell(12).SetCellValue("供应商");

            int index = 1;
            foreach (var item in list.List)
            {
                rowl = sheet.CreateRow(index);
                rowl.CreateCell(0).SetCellValue(item.ProductNo);
                rowl.CreateCell(1).SetCellValue(item.ProductName);
                rowl.CreateCell(2).SetCellValue(item.ManuNo);
                rowl.CreateCell(3).SetCellValue(item.InsideNo);
                rowl.CreateCell(4).SetCellValue(item.ProductSpec);
                rowl.CreateCell(5).SetCellValue(item.ProductTypeName);
                rowl.CreateCell(6).SetCellValue(item.ProductSpec);
                rowl.CreateCell(7).SetCellValue((int)item.Online);
                rowl.CreateCell(8).SetCellValue((int)item.GoOnline);
                rowl.CreateCell(9).SetCellValue(item.ProductWrap);
                rowl.CreateCell(10).SetCellValue((double)item.ProductPrice);
                rowl.CreateCell(11).SetCellValue(item.ProductWeight);
                rowl.CreateCell(12).SetCellValue(item.VendorId);
                index++;
            }
            byte[] bytes;

            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                bytes = ms.ToArray();
            }
            string mapPath = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bytes, mapPath, "产品管理信息.xlsx");
        }



        /// <summary>
        /// 导出产品类别管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ProductTypeExcel()
        {
            var list = await mediator.Send(new ProductTypeSelCommand());

            IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("产品类别管理");

            IRow rowl = sheet.CreateRow(0);
            rowl.CreateCell(0).SetCellValue("产品类别编号");
            rowl.CreateCell(1).SetCellValue("产品类别名称");
            rowl.CreateCell(2).SetCellValue("上级类别");
            rowl.CreateCell(3).SetCellValue("备注");
           

            int index = 1;
            foreach (var item in list)
            {
                rowl = sheet.CreateRow(index);
                rowl.CreateCell(0).SetCellValue(item.ProductTypeNo);
                rowl.CreateCell(1).SetCellValue(item.ProductTypeName);
                rowl.CreateCell(2).SetCellValue(item.ProductSuperior);
                rowl.CreateCell(3).SetCellValue(item.ProductRemarks);
                index++;
            }
            byte[] bytes;

            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                bytes = ms.ToArray();
            }
            string mapPath = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bytes, mapPath, "产品类别管理信息.xlsx");
        }

        /// <summary>
        /// 显示产品单位连表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ProductUnitDto>>> getProuductUint()
        {
            ApiResult<List<ProductUnitDto>> result = new ApiResult<List<ProductUnitDto>>();

            try
            {
                logger.LogInformation("显示产品单位");
                result.Data = await mediator.Send(new ProductUnitShowCommand());
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                throw;
            }
        }
    }
}
